using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CommonLibrary.DBUtility;
using CommonLibrary.Model;

namespace CommonLibrary.DAL
{
    public class Template
    {
        public bool Exsits(int TempID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@TempID",SqlDbType.Int)
            };
            parameters[0].Value = TempID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Template_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public bool Insert(TemplateInfo Template)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@UserID",SqlDbType.Int),
                new SqlParameter("@TempName",SqlDbType.VarChar,50),
                new SqlParameter("@TempContent",SqlDbType.VarChar,500)
            };
            parameters[0].Value = Template.UserID;
            parameters[1].Value = Template.TempName;
            parameters[2].Value = Template.TempContent;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Template_Insert", parameters);
                return rowAffected > 0;
            }
        }
        public bool Update(TemplateInfo Template)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@TempID",SqlDbType.Int),
                new SqlParameter("@UserID",SqlDbType.Int),
                new SqlParameter("@TempName",SqlDbType.VarChar,50),
                new SqlParameter("@TempContent",SqlDbType.VarChar,500)
            };
            parameters[0].Value = Template.TempID;
            parameters[1].Value = Template.UserID;
            parameters[2].Value = Template.TempName;
            parameters[3].Value = Template.TempContent;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Template_Update", parameters);
                return rowAffected > 0;
            }
        }
        public bool Delete(int TempID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@TempID",SqlDbType.Int)
            };
            parameters[0].Value = TempID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Template_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public TemplateInfo GetTemplate(int TempID)
        {
            TemplateInfo Template = null;
            SqlParameter[] parameters = {
                new SqlParameter("@TempID",SqlDbType.Int)
            };
            parameters[0].Value = TempID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Template_GetDetail", parameters);
                if (dr.Read())
                {
                    Template = new TemplateInfo();
                    Template.TempID = dr.GetInt32(0);
                    Template.UserID = dr.GetInt32(1);
                    Template.TempName = dr.GetString(2);
                    Template.TempContent = dr.GetString(3);
                }
                return Template;
            }
        }
        public List<TemplateInfo> GetList(string StrWhere)
        {
            List<TemplateInfo> Templates = new List<TemplateInfo>();
            TemplateInfo Template = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Template_GetList", parameters);
                while (dr.Read())
                {
                    Template = new TemplateInfo();
                    Template.TempID = dr.GetInt32(0);
                    Template.UserID = dr.GetInt32(1);
                    Template.TempName = dr.GetString(2);
                    Template.TempContent = dr.GetString(3);
                    Templates.Add(Template);
                }
                return Templates;
            }
        }
    }
}
